from kombu import Queue, Exchange
from datetime import timedelta
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = '51)fj#mnm%-ctk6+$eah%5=rv(rsl(2k5ntivjgt&7f3=%a7j+'

DEBUG = False

ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "corsheaders",
    "rest_framework",
    'rest_framework.authtoken',
    "apps.user",
    "apps.gateway",
    "apps.missions",
    "apps.scripts",
    "apps.new_gateway",
    "apps.file",
    "apps.fileconfig",
    "apps.manageFile",
    "apps.history"
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    "corsheaders.middleware.CorsMiddleware",
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    "util.md.Md1",
]
CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)

CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
    "X-Access-Token"
)

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
    ]
}

ROOT_URLCONF = 'config.urls'

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = "Asia/Shanghai"

USE_I18N = True

USE_L10N = True

USE_TZ = False

STATIC_URL = '/static/'

CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()

CELERY_RESULT_SERIALIZER = 'json'  # 读取任务结果一般性能要求不高，所以使用了可读性更好的JSON

CELERY_TASK_RESULT_EXPIRES = 60 * 60 * 24  # 任务过期时间，不建议直接写86400，应该让这样的magic数字表述更明显

CELERY_ACCEPT_CONTENT = ['json']  # 指定接受的内容类型

CELERY_QUEUES = (
    Queue('no_file_queue', Exchange('no_file_queue'), routing_key='no_file_queue'),
    Queue('short_file_queue', Exchange('short_file_queue'), routing_key='short_file_queue'),
    Queue('long_file_queue', Exchange('long_file_queue'), routing_key='long_file_queue'),
    Queue('beat', Exchange('beat'), routing_key='beat'),
    Queue('default', Exchange('default'), routing_key='default'),
)
CELERY_ROUTES = {
    "reboot_gateway_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "check_sim_charge": {"queue": "beat", "routing_key": "beat"},
    "poweroff_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "query_sim": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "new_gateway_reboot_gateway_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "new_gateway_poweroff_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "reboot_sensor_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "read_gateway_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "new_gateway_read_gateway_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "read_sensor_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "get_data_task": {"queue": "no_file_queue", "routing_key": "no_file_queue"},
    "upload_gateway_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "upload_sensor_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "agreement_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "main_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "config_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "new_gateway_agreement_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "new_gateway_main_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "new_gateway_config_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "down_script_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "down_deploy_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "active_task": {"queue": "short_file_queue", "routing_key": "short_file_queue"},
    "log_task": {"queue": "long_file_queue", "routing_key": "long_file_queue"}
}
CELERYD_CONCURRENCY = 20
CELERYD_FORCE_EXECV = True
CELERY_DEFAULT_QUEUE = 'default'
CELERY_DEFAULT_EXCHANGE = 'default'
CELERY_DEFAULT_ROUTING_KEY = 'default'
CELERYBEAT_SCHEDULE = {
    "check_sim_charge": {
        "task": "check_sim_charge",
        "schedule": timedelta(seconds=60*60),
        "args": ()
    }
}

# token过期时间
TOKEN_TIME = 24 * 60 * 60

# 重启网关
REBOOT_GATEWAY = 1

# 重启传感器
REBOOT_SENSOR = 2

# 升级网关
UPDATE_GATEWAY = 3

# 升级传感器
UPDATE_SENSOR = 4

# 读网关版本号
READ_GATEWAY = 5

# 读传感器版本号
READ_SENSOR = 6

# 上传配置文件
UPLOAD_DEPLOY = 7

# 上传日志文件
UPLOAD_LOG = 8

# 下载脚本文件
DOWN_SCRIPT = 9

# 下载配置文件
DOWN_DEPLOY = 10

# 采集数据
GET_DATA = 11

# 升级协议文件
AGREEMENT_GATEWAY = 12

# 主程序升级
MAIN_GATEWAY = 13

# tac31配置文件
CONFIG_GATEWAY = 14

# tac31关机指令
POWEROFF = 15

# 查询卡状态
QUERY_SIM = 16

# 查询卡状态
ADD_SIM = 17

# 网关clientid配置
ID_HEAD = "TIAID"
ID_END = "-2"

# tac03
NEW_GATEWAY_NAME = "tia"

# json解释文件名称
JSON_EXPLAIN_FILE = "json_explain_file.json"

TRANSID_NUMBER = "transid_number"
